package com.sinoeyes.controller.system;

import java.util.List;

import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.constant.UserConstants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysDeptFilter;
import com.sinoeyes.system.service.ISysDeptService;
import com.sinoeyes.system.vo.SysDeptVo;
import com.sinoeyes.system.vo.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * 部门信息
 */
@RestController
@RequestMapping("/system/dept")
@Api(value = "部门信息 API", tags = {"部门信息"})
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list")
    @ApiOperation("获取部门列表")
    public Result<List<SysDeptVo>> list(SysDeptFilter deptFilter)
    {
        List<SysDeptVo> depts = deptService.selectDeptList(deptFilter);
        return Result.success(deptService.buildDeptTree(depts));
    }

    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    @ApiOperation("根据部门编号获取详细信息")
    public Result<SysDeptVo> getInfo(@PathVariable Long deptId)
    {
        return Result.success(deptService.selectDeptById(deptId));
    }

    /**
     * 获取部门下拉树列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping("/treeselect")
    @ApiOperation("获取部门下拉树列表")
    public Result<List<TreeSelect>> treeselect(SysDeptFilter deptFilter)
    {
        List<SysDeptVo> depts = deptService.selectDeptList(deptFilter);
        return Result.success(deptService.buildDeptTreeSelect(depts));
    }

    /**
     * 加载对应角色部门列表树
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    @ResponseBody
    @ApiOperation("加载对应角色部门列表树")
    public Result<List<Integer>> roleDeptTreeselect(@PathVariable("roleId") Long roleId)
    {
        return Result.success(deptService.selectDeptListByRoleId(roleId));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增部门")
    public Result<Integer> add(@RequestBody SysDeptVo dept)
    {
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setCreateBy(SecurityUtils.getUsername());
        return Result.success(deptService.insertDept(dept));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改部门")
    public Result edit(@RequestBody SysDeptVo dept)
    {
        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (dept.getParentId().equals(dept.getDeptId()))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        }
        dept.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    @ApiOperation("删除部门")
    public Result remove(@PathVariable Long deptId)
    {
        if (deptService.hasChildByDeptId(deptId))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"部门存在用户,不允许删除");
        }
        return Result.success(deptService.deleteDeptById(deptId));
    }
}
